/*
 *  Copyright (C) 2011 felixtheripper <felixtheripper_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/**
 *
 * @author felixtheripper
 *
 * email: felixtheripper_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */


package org.myorg.ctm.etk.gui.swg.display.optionpane;

import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import javax.swing.JOptionPane;
import org.myorg.ctm.etk.gui.swg.display.dialog.*;
import org.myorg.shr.gui.swg.util.logging.FileHandlerShr;

import org.myorg.ctm.etk.gui.swg.util.property.PrpNamingEtk;

/**
 *
 * 
 */


abstract public class EtkOptionPaneAbs extends JOptionPane
{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;


	final static private String _STR_copyright =
		"Licensed under GNU LGPL" +
	    "\n" +
	    "\n\n" +
	    "More:" +
	    "\n" +
	    "  http://code.google.com/p/ettiket/";


	final static private String _STR_BTN_OK = "OK";
	final static private String _STR_BTN_CANCEL = "CANCEL";
	final static private String _STR_BTN_CLOSE = "CLOSE";
	final static private String _STR_TITLE_ERROR = "Error";

	protected EtkOptionPaneAbs(
			Object[] objsArray,
			int intTypeMessage,
			Object[] objsOption)
	{
		super(objsArray, intTypeMessage, JOptionPane.YES_NO_CANCEL_OPTION,
				null, objsOption, objsOption[0]);
	}



	static public String s_showDialogQuestionInput(
			Component cmpParent, // nil value allowed
			String strTitle,
			String strBody,
			Object[] objsOption,
			Object objInitialValue
			)
	{
            return (String) JOptionPane.showInputDialog(
            cmpParent, strBody, strTitle, JOptionPane.QUESTION_MESSAGE, null,
            objsOption, objInitialValue);
	}

        

        static public boolean s_showDialogBringSecWin(
			Component cmpParent,
			String strTitleLong,
			String strBody)
        {
            Object[] objsOption = { EtkOptionPaneAbs._STR_BTN_OK, "Bring secondary windows to front"};

            int intReply = JOptionPane.showOptionDialog(cmpParent, strBody, strTitleLong,
                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
                null, objsOption, objsOption[0]);

            if (intReply == 1) // cancelButton or "X" on topRight
            {
                return true;
            }

            return false;
        }

	static public boolean s_showDialogDontShowAgain(
			Component cmpParent,
			String strTitleLong,
			String strBody)
        {
            Object[] objsOption = { EtkOptionPaneAbs._STR_BTN_OK, "Don't show this again"};

            int intReply = JOptionPane.showOptionDialog(cmpParent, strBody, strTitleLong,
                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
                null, objsOption, objsOption[0]);

            if (intReply == 1) // cancelButton or "X" on topRight
            {
                return true;
            }

            return false;
        }

	static public boolean s_showDialogConfirm(
			Component cmpParent,
			String strTitleLong,
			String strBody)
    {
        Object[] objsOption = { EtkOptionPaneAbs._STR_BTN_OK, EtkOptionPaneAbs._STR_BTN_CANCEL};

        int intReply = JOptionPane.showOptionDialog(cmpParent, strBody, strTitleLong,
            JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
            null, objsOption, objsOption[0]);

        if (intReply==1 || intReply==-1) // cancelButton or "X" on topRight
        {
            return false;
        }

        if (intReply != 0)
        {
        	// tbrl!
        	return false;
        }

        return true;
    }


	static public void s_showDialogWarning(
			 Component cmpParent,
			 String strBody)
	 {
		String strTitleLong = System.getProperty(PrpNamingEtk.STR_PRP_APPLI_TITLE);
		strTitleLong += " - " + "warning";

		JOptionPane.showMessageDialog(cmpParent,
				strBody,
				strTitleLong,
	            JOptionPane.WARNING_MESSAGE);
	 }

	static public void s_showDialogAbout(
			 Component cmpParent)
	 {

		String strWhat = "About " + System.getProperty(PrpNamingEtk.STR_PRP_APPLI_TITLE) + ":";
		strWhat +="\n";

		strWhat += "  version # ";
		strWhat += System.getProperty(PrpNamingEtk.STR_PRP_APPLI_VERSION);

		strWhat +=" \n\n";

		strWhat += EtkOptionPaneAbs._STR_copyright;

		

                // ----
             
                try
                {
                    File fle = FileHandlerShr.s_getOrCreateVersionDir();

                    if (fle != null)
                    {
               
                        strWhat +=" \n\n";
                        String strCacheDir = "Cache folder: ";
                        strCacheDir += "\n ";
                        strCacheDir += fle.getCanonicalPath();
                        strWhat += strCacheDir;
                    }
                }

                catch (Exception ex)
                {
                    // don't care
                }

		strWhat +=" \n\n";
                

                // ----
                //Icon icon = null;
		//ImageIcon iin = ImageIconFinder.s_get(EtkOptionPaneAbs._STR_NAME_LOGO_GFR);

                //if (iin != null)
                  //  icon = (Icon) iin;


		String strTitleLong = System.getProperty(PrpNamingEtk.STR_PRP_APPLI_TITLE);
		strTitleLong += " - " + "about";

		JOptionPane.showMessageDialog(cmpParent, strWhat, strTitleLong, JOptionPane.INFORMATION_MESSAGE/*, icon*/);

	 }

	static public void s_showDialogInfo(
			 Component cmpParent,
			 String strBody)
	 {
		String strTitleLong = System.getProperty(PrpNamingEtk.STR_PRP_APPLI_TITLE);
		strTitleLong += " - " + "information";



		JOptionPane.showMessageDialog(cmpParent,
				strBody,
				strTitleLong,
	            JOptionPane.INFORMATION_MESSAGE);
	 }

	 static public void s_showDialogError(
			 Component cmpParent,
			 String strBody)
	 {
		String strTitleAppli = System.getProperty(PrpNamingEtk.STR_PRP_APPLI_TITLE);


	        String strTitle = null;

	        if (strTitleAppli == null) // should never appear
	            strTitle = EtkOptionPaneAbs._STR_TITLE_ERROR;
	        else
	            strTitle = strTitleAppli + " - " + EtkOptionPaneAbs._STR_TITLE_ERROR;

	        File fle = new File(FileHandlerShr.s_getPathAbsFileLogJava());

	        String strPathAbsLog = null;

	        if (fle.exists())
	        {

                    if (fle.canRead())
                    {
                            if (fle.length() > 0)
                                    strPathAbsLog = fle.getAbsolutePath();
                    }
	        }

	        Object[] objsOption = null;

	        if (strPathAbsLog == null)
	        {
	            objsOption = new Object[1];
	        }

	        else
	        {
	            objsOption = new Object[2];
	            objsOption[1] = "SHOW SESSION LOG";

	            strBody += "\n\n";
	            strBody += "Session log file path:";
	            strBody += "\n" + "  " + strPathAbsLog + "\n";
	        }

	        objsOption[0] = EtkOptionPaneAbs._STR_BTN_CLOSE;

	        int intRetVal = JOptionPane.showOptionDialog(cmpParent, strBody, strTitle,
	                JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
	                null, objsOption, objsOption[0]);

	        if (intRetVal == 1)
	        {
	            DlgEViewSrcFileTxtSys dlg = new  DlgEViewSrcFileTxtSys(cmpParent, strTitleAppli);

	            if (! dlg.init())
	            {
	                // TBRL
	                return;
	            }

	            Dimension dim = new Dimension(500, 400);
	            dlg.setPreferredSize(dim);

	            if (! dlg.show(fle))
	            {
	                // TBRL
	                return;
	            }
	        }
	    }

}
